import os
os.environ["CUDA_VISIBLE_DEVICES"] = "0"

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from contextlib import asynccontextmanager
from app.api.routers import router as api_router
from app.core.model_manager import model_manager

@asynccontextmanager
async def lifespan(app: FastAPI):
    """应用生命周期管理"""
    print("正在预加载模型...")
    model_manager.preload_models()
    print("模型预加载完成")
    yield
    # 这里可以添加清理代码（如果需要的话）

app = FastAPI(
    title="FireRedASR API",
    description="FireRedASR 语音识别 API 服务",
    version="1.0.0",
    lifespan=lifespan
)

# 配置 CORS
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# 注册路由
app.include_router(api_router, prefix="/api/v1")

@app.get("/")
async def root():
    return {"message": "欢迎使用 FireRedASR API 服务"} 




if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=10000)